import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'target_page.dart';

void main() {
  runApp(RootApp());
}

// runApp(
//       GetMaterialApp(
//         home: HomePage(),
//         routes: {
//           'target': (context) => TargetPage(),
//         },
//       ),
//     );

class RootApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //使用 GetX第一步
    return GetMaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
      debugShowCheckedModeBanner: false,
      routes: {
        '/target': (context) => TargetPage(),
      },
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("子页面"),
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: Column(
          children: [
            ElevatedButton(
              child: Text("传统方式动态打开页面"),
              onPressed: routePush,
            ),
            ElevatedButton(
              child: Text("传统方式静态打开页面"),
              onPressed: routePushName,
            ),
            ElevatedButton(
              child: Text("传统方式打开新页面，替换当前页面"),
              onPressed: routePushAndReplace,
            ),
            ElevatedButton(
              child: Text("传统方式打开新页面，替换之前所有页面"),
              onPressed: routePushAndReplaceAll,
            ),
            ElevatedButton(
              child: Text("getX动态打开页面"),
              onPressed: getXPush,
            ),
            ElevatedButton(
              child: Text("getX静态打开页面"),
              onPressed: getXPushName,
            ),
            ElevatedButton(
              child: Text("getX打开新页面，替换当前页面"),
              onPressed: getXPushAndReplace,
            ),
            ElevatedButton(
              child: Text("getX打开新页面，替换之前所有页面"),
              onPressed: getXPushAndReplaceAll,
            ),
          ],
        ),
      ),
    );
  }

  routePush() {
    Navigator.of(context)
        .push(new MaterialPageRoute(builder: (BuildContext context) {
      return TargetPage();
    })).then((value) {
      print("页面返回的值 $value");
    });
    // Navigator.of(context).push(
    //   new MaterialPageRoute(
    //     builder: (BuildContext context) {
    //       return new TargetPage();
    //     },
    //   ),
    // ).then((value) {
    //   //获取上一个页面的数据
    // });
  }

  routePushName() {
    Navigator.of(context).pushNamed("/target");
  }

  routePushAndReplace() {
    Navigator.of(context).pushReplacement(
      new MaterialPageRoute(
        builder: (BuildContext context) {
          return TargetPage();
        },
      ),
    );
  }

  routePushAndReplaceAll() {
    Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(
      builder: (BuildContext context) {
        return TargetPage();
      },
    ), (route) => false);
  }

  getXPush() async {
    var value = await Get.to(() => new TargetPage());
    print("页面返回的值 $value");
  }

  getXPushName() {
    Get.toNamed("/target");
  }

  getXPushAndReplace() {
    Get.off(() => TargetPage());
  }

  getXPushAndReplaceAll() {
    Get.offAll(() => TargetPage());
  }
}
